﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography;
using Cabal.Kernel.Provider;
using Cabal.Kernel.Cryptilogy;

namespace test
{
    public partial class frmConst : Form
    {
        public frmConst()
        {
            InitializeComponent();
        }

        private void listView1_DoubleClick(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count == 1)
            {
                frmEditor editor = new frmEditor(listView1.SelectedItems[0].SubItems[0].Text,listView1.SelectedItems[0].SubItems[1].Text, listView1.SelectedItems[0].SubItems[2].Text);

                switch (editor.ShowDialog())
                {
                    case DialogResult.OK:
                        {
                            listView1.SelectedItems[0].SubItems[0].Text = editor.UserTag;
                            listView1.SelectedItems[0].SubItems[1].Text = editor.Key;
                            listView1.SelectedItems[0].SubItems[2].Text = editor.Value;
                            break;
                        }
                    case DialogResult.No: //remove
                        {
                            listView1.Items.Remove(listView1.SelectedItems[0]);
                            break;
                        }
                }
            }
        }

        RSACrypt rsa;// = new RSACrypt();
        private void btnTEn_Click(object sender, EventArgs e)
        {
            rsa = new RSACrypt(txtEnKey.Text, txtDeKey.Text, txtModNum.Text);
            string[] result = rsa.EncryptBase64Blocks(txtTest.Text);
            StringBuilder sb = new StringBuilder();
            foreach (string i in result)
            {
                sb.Append(i);
                sb.Append(",");
            }
            sb.Remove(sb.Length - 1, 1);
            txtTest.Text = sb.ToString();
        }

        private void btnTDe_Click(object sender, EventArgs e)
        {
            rsa = new RSACrypt(txtEnKey.Text, txtDeKey.Text, txtModNum.Text);
            string[] data = txtTest.Text.Split(',');
            txtTest.Text = rsa.DecryptBase64Blocks(data);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            frmEditor editor = new frmEditor();
            editor.Show();
        }

        private void lnkAdd_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            ListViewItem item = new ListViewItem();
            item.SubItems[0].Text = "defult";
            item.SubItems.Add("Key");
            item.SubItems.Add("");

            if (listView1.SelectedItems.Count == 1)
            {
                int index = listView1.SelectedItems[0].Index;
                listView1.Items.Insert(index, item);
            }
            else
            {
                listView1.Items.Add(item);
            }
        }

        private void lnkRemove_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (listView1.SelectedItems.Count == 1)
                listView1.Items.Remove(listView1.SelectedItems[0]);
        }

        private void lnkAddnc_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            ListViewItem item = new ListViewItem();
            item.SubItems[0].Text = "noncrypt";
            item.SubItems.Add("Key");
            item.SubItems.Add("");

            if (listView1.SelectedItems.Count == 1)
            {
                int index = listView1.SelectedItems[0].Index;
                listView1.Items.Insert(index, item);
            }
            else
            {
                listView1.Items.Add(item);
            }
        }


        Cabal.Kernel.CabalSoul kernel = new Cabal.Kernel.CabalSoul(true);

        private void btnRead_Click(object sender, EventArgs e)
        {
            listView1.Items.Clear();

            var r = ConstProvider.BaseCryptProviders(txtDeKey.Text, txtModNum.Text);
            foreach (var i in r)
                kernel.ConstControl.AddCryptProvider(i);

            List<CryptException> tmp;
            kernel.ConstControl.Decrypt(out tmp);

            foreach (var i in kernel.ConstControl.Items)
                listView1.Items.Add(new ListViewItem(new string[] { i.CryptMethod, i.Key, i.Value }));
        }
    }
}
